<if condition="$since eq NULL">
<ul id="myTab" class="nav nav-tabs">
	<li class="active">
		<a href="#home" data-toggle="tab" style="margin-right: 0;width: 95px;">选择客户</a>
	</li>
	<li><a href="#ios" data-toggle="tab" style="margin-right: 0;width: 95px;">添加客户</a></li>
</ul>
<div id="myTabContent" class="tab-content">
	<div class="tab-pane fade in active" id="home">
		<form class="add-custome-info2">
			<input type="hidden" name="pid" value="{$f_pid}">
			<input type="hidden" name="way" value="{$way}">
			<input type="hidden" name="since" value="{$since}">
			<table class="table table-bordered">
				<tr>
                    <td width="12%">项目客户</td>
                    <td width="39%" colspan="3">
                        <select name="user" class="form-control select-add w300 customers" autocomplete="off">
                            <option value="-10" selected>请选择</option>
                            <foreach name="data" item="c">
                                <option value="{$c['id']}">{$c['username']}<if condition="$c['company_name'] neq NULL">【{$c['company_name']}】</if></option>
                            </foreach>
                        </select>
                        <span class="error-item">必填项</span>
                    </td>
                </tr>
				<tr class="change">
					<td width="12%">姓名</td>
					<td width="39%">
						
					</td>
					<td width="10%">性别</td>
					<td width="39%">
					</td>
				</tr>
				<tr class="change">
					<td>公司名称</td>
					<td>
						
					</td>
					<td>职位</td>
					<td>
						
					</td>
				</tr>
				<tr class="change">
					<td>手机号</td>
					<td>
						
					</td>
					<td>座机号</td>
					<td>
						
					</td>
				</tr>
				<tr class="change">
					<td>QQ</td>
					<td>
						
					</td>
					<td>邮箱</td>
					<td>
						
					</td>
				</tr>
				<tr class="change">
					<td>地址</td>
					<td colspan="3">
						
					</td>
				</tr>
				<tr class="change">
					<td>其它信息</td>
					<td colspan="3">
						
					</td>
				</tr>
			</table>
			<div class="clearboth mt10">
				<div class="right">
					<input type="button" class="btn btn-choise" value="提交">
				</div>
			</div>
		</form>
	</div>
	</if>
	<div <if condition="$since eq NULL">class="tab-pane fade"</if> id="ios">
		<form class="add-custome-info">
			<input type="hidden" name="pid" value="{$f_pid}">
			<input type="hidden" name="way" value="{$way}">
			<input type="hidden" name="since" value="{$since}">
			<table class="table table-bordered">
				<tr>
					<td class="fbold" width="12%"><strong class="red">*</strong>姓名</td>
					<td width="39%">
						<input type="text" name="username" class="form-control required-text width100">
						<span class="error-item">必填项</span>
					</td>
					<td class="fbold" width="10%"><strong class="red">*</strong>性别</td>
					<td width="39%">
						<label class="radio-inline"><input type="radio" name="gender" value='男' autocomplete="off">男</label>
						<label class="radio-inline"><input type="radio" name="gender" value='女' autocomplete="off">女</label>
						<span class="error-item">必填项</span>
					</td>
				</tr>
				<tr>
					<td class="fbold"><strong class="red">*</strong>公司名称</td>
					<td>
						<input type="text" name="company_name" class="form-control required-text">
						<span class="error-item">必填项</span>
					</td>
					<td class="fbold">职位</td>
					<td>
						<input type="text" name="job" class="form-control">
					</td>
				</tr>
				<tr>
					<td class="fbold">手机号</td>
					<td>
						<input class="form-control" type="text" name="mobilephone" maxlength="11" placeholder="手机号或座机号必须填写一项">
						<div class="error-item">请填写正确的手机号</div>
					</td>
					<td class="fbold">座机号</td>
					<td>
						<input class="form-control" type="text" name="telephone" placeholder="手机号或座机号必须填写一项">
						<div class="error-item">请填写正确的座机号</div>
					</td>
				</tr>
				<tr>
					<td class="fbold">QQ</td>
					<td>
						<input type="text" name="qq" class="form-control">
						<div class="error-item">请填写正确的QQ</div>
					</td>
					<td class="fbold">邮箱</td>
					<td>
						<input type="text" name="email" class="form-control">
						<div class="error-item">请填写正确的邮箱</div>
					</td>
				</tr>
				<tr>
					<td class="fbold"><strong class="red">*</strong>地址</td>
					<td colspan="3">
						<select name="province_id" class="w150 required-select">
							<option value="-10">省</option>
							<foreach name="province_list" item="vo">
								<option value="{$vo['id']}">{$vo['name']}</option>
							</foreach>
						</select>
						<select name="city_id" class="w150 required-select marginLeft10">
							<option value="-10">市</option>
						</select>
						<span class="error-item">省市必填</span>
						<input class="form-control" style="width: 274px" type="text" name="address" placeholder="详细地址（可不填）">
					</td>
				</tr>
				<tr>
					<td class="fbold">其它信息</td>
					<td colspan="3">
						<input class="form-control" type="text" name="remark" style="width:580px;">
					</td>
				</tr>
			</table>
			<div class="clearboth mt10">
				<div class="right">
					<span class="ajax_error_tip2 error-item hide mr10"></span><input type="button" class="btn btn-card" value="提交">
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript">
(function(){
	$("[name='user']").select2();
	$('#myTab a').click(function (e) {
		e.preventDefault()
		$(this).tab('show');
	})
	var custome = $("form.add-custome-info"),
		custome2 = $("form.add-custome-info2");

	custome.find("select").select2();
	//省市联动
	custome.find("[name='province_id']").change(function(){
		var id = $(this).val();
		$.ajax({
			type:"POST",
			dataType:'json',
			data:{'region':id,'type':1,'bool':1},
			url:"__APP__/Communal/get_province_city",
			success:function(data){
				var option = "";
				if(data){
					option += "<option value='-10'>请选择</option>";
					if(data){
						for(var i=0;i<data.length;i++){
							option += "<option value="+data[i].id+">"+data[i].name+"</option>";
						}
					}
				}else{
					option += "<option value='-10'>市</option>";
				}
				custome.find("[name='city_id']").html(option).select2();
			}
		});
	});
	//选择客户
	$("[name='user']").on("change",function(){
		var mid = $(this).val(),
			table = $(this).parents('table');
		if(mid==-10){
			var html = "<tr class='change'><td width='12%'>姓名</td><td width='39%'></td><td width='10%'>性别</td><td width='39%'></td></tr><tr class='change'><td>公司名称</td><td></td><td>职位</td><td></td></tr><tr class='change'><td>手机号</td><td></td><td>座机号</td><td></td></tr><tr class='change'><td>QQ</td><td></td><td>邮箱</td><td></td></tr><tr class='change'><td>地址</td><td colspan='3'></td></tr><tr class='change'><td>其它信息</td><td colspan='3'></td></tr>";
			table.find('.change').remove();
			table.append(html);
		}else{
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:{"id":mid},
				url:"__APP__/Project/project_card_detail",
				success:function(data){
					var html = "<tr class='change'><td width='12%'>姓名</td><td width='39%'>";
					html+=data.username ? data.username : "";
					html+="</td>";
					html+="<td width='10%'>性别</td><td width='39%'>";
					html+=data.gender ? data.gender : "";
					html+="</td></tr>";
					html+="<tr class='change'><td>公司名称</td><td>";
					html+=data.company_name ? data.company_name : "";
					html+="</td><td>职位</td><td>";
					html+=data.job ? data.job : "";
					html+="</td></tr>";
					html+="<tr class='change'><td>手机号</td><td>";
					html+=data.mobilephone ? data.mobilephone : "";
					html+="</td><td>座机号</td><td>";
					html+=data.telephone ? data.telephone : "";
					html+="</td></tr>";
					html+="<tr class='change'><td>QQ</td><td>";
					html+=data.qq ? data.qq : "";
					html+="</td><td>邮箱</td><td>";
					html+=data.email ? data.email : "";
					html+="</td></tr>";
					html+="<tr class='change'><td>地址</td><td colspan='3'>";
					if(data.province){
						html+=data.province+"-"+data.city;
						html+=data.address ? "-"+data.address : "";
					}else{
						html+=data.address ? data.address : "";
					}
					html+="</td></tr>";
					html+="<tr class='change'><td>其它信息</td><td colspan='3'>";
					html+=data.remark ? data.remark : "";
					html+="</td></tr>";
					table.find('.change').remove();
					table.append(html);
				}
			});
		}
	});
	//表单验证
	custome.find(".btn-card").click(function(){
		var tag = true;
		custome.find(".required-text").each(function(){
			var $val = $(this).val();
			if($val=="" || /^\s+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		custome.find("select.required-select").each(function(){
			var $val = $(this).val();
			if($val==-10){
				$(this).nextAll(".error-item").show();
				tag = false;
			}
		});
		custome.find("[name='mobilephone']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^1[3|4|5|7|8]\d{9}$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		custome.find("[name='telephone']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^(\d{3,4}\-?)?\d{7,8}(\-\d{3,4})?$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		custome.find("[name='email']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		custome.find("[name='qq']").each(function(){
			var $val = $(this).val();
			if($val!="" && !/^[1-9]\d{4,10}$/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		if(custome.find("[name='mobilephone']").val()=="" && custome.find("[name='telephone']").val()==""){
			$("#customeModal").find(".ajax_error_tip2").text("手机号或座机号必须填写一项").show();
			tag = false;
		}		
		if(custome.find("[name='gender']:checked").length==0){
			custome.find("[name='gender']").parent("label").nextAll(".error-item").show();
			tag = false;
		}

		if(tag){
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:custome.serialize(),
				url:"__APP__/Project/project_card_doadd",
				success:function(data){
					if(data.status==0){
						$("#customeModal").find(".ajax_error_tip2").text(data.msg).show();
					}else{
						var action = "{$action}",
							since = "{$since}";
						
						if(action){
							$("#customeModal").hide();
							success_pop(data.status,data.msg);
						}else{
							if(since==2){
								var length = data['customers'].length,
									options = "<option value='-10'>请选择联系人</option>",
									add_id = data['id'];
								$("#customeModal").modal('hide');
								for(var i=0;i<length;i++){
									options += "<option value="+data['customers'][i]['id']+">"+data['customers'][i]['username'];
									options += "【公司:"+data['customers'][i]['company_name']+"】";
									options += "</option>";
								}
								$("select.customers").html(options).val(add_id).select2().nextAll(".add_cus").hide().end().nextAll(".error-item").hide();
							}else{
								var html = "<div class='form-item user-add clearfix'><div class='input-label'></div><div class='input-item'><span>"+ data.cus['username'] +" 【"+data.cus['company_name']+"】</span><a href='javascript:;' class='pname-delete ml10' data-toggle='tooltip' data-placement='bottom' title='删除'><i class='glyphicon glyphicon-trash'></i></a><input type='hidden' name='hide_val[]' value='"+ data.cus['id'] +"'></div></div>",
									formmain = $('.add-cont');
								formmain.append(html);
								$("#customeModal").modal('hide');
							}
						}
					}
				}
			});
		}
	});
	custome2.find(".btn-choise").click(function(){
		var tag = true;
		custome2.find("[name='user']").each(function(){
			var $val = $(this).val();
			if($val==-10){
				$(this).nextAll(".error-item").show();
				tag = false;
			}
		});
		if(tag){
			$("#customeModal").modal('hide');
			var usertxt = $("[name='user']").find("option:selected").text(),
				userval = $("[name='user']").find("option:selected").val(),
				html = "<div class='form-item user-add clearfix'><div class='input-label'></div><div class='input-item'><span>"+ usertxt +"</span><a href='javascript:;' class='pname-delete ml10' data-toggle='tooltip' data-placement='bottom' title='删除'><i class='glyphicon glyphicon-trash'></i></a><input type='hidden' name='hide_val[]' value='"+ userval +"'></div></div>",
			formmain = $('.add-cont');
			formmain.append(html);
			$('.add_cus').next(".error-item").hide();
		}
	});
	//隐藏错误提示
	custome.find("[name='mobilephone'],[name='telephone']").focus(function(){
		$(this).next(".error-item").hide();
		$("#customeModal").find(".ajax_error_tip2").text("").hide();
	});
	custome.find(".required-text,[name='qq'],[name='email']").focus(function(){
		$(this).next(".error-item").hide();
	});
	custome.find("select.required-select").change(function(){
		$(this).nextAll(".error-item").hide();
		$("#customeModal").find(".ajax_error_tip2").text("").hide();
	});
	custome.find("[name='gender']").click(function(){
		$(this).parent("label").nextAll(".error-item").hide();
	});
	$('#customeModal').on("hidden",function(){
		$(this).find(".ajax_error_tip2").text("").hide();
	});
})();
</script>